<?php

namespace App\Models;
use App\Models\OrderDetail;

class rewardOrder extends Base
{
    protected $table = 'reward_order';

   
    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;
    const STATUS_5 = 5;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag ">进行中</span>',
    	self::STATUS_2 => '<span class="el-tag el-tag--info">已提交</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">任务完成</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--danger">任务失败</span>',
    	self::STATUS_5 => '<span class="el-tag el-tag--danger">任务超时</span>'
   ];
    const STATUS_TEXT_1 = [
    		self::STATUS_1 => '进行中',
    		self::STATUS_2 => '已提交',
    		self::STATUS_3 => '任务完成',
    		self::STATUS_4 => '任务失败',
    		self::STATUS_5 => '任务超时'
    ];
    
    //  审核状态 0默认  1审核通过、完成  2、审核不通过，完成  3、赏金猎人申述 4、悬赏主申述
    const CHECK_STATUS_0 = 0;
    const CHECK_STATUS_1 = 1;
    const CHECK_STATUS_2 = 2;
    const CHECK_STATUS_3 = 3;
    const CHECK_STATUS_4 = 4;
   
    
    const R_STATUS_TEXT = [
    	self::CHECK_STATUS_0 => '<span class="el-tag">正常</span>',
    	self::CHECK_STATUS_1 => '<span class="el-tag el-tag--info">审核通过</span>',
    	self::CHECK_STATUS_2 => '<span class="el-tag el-tag--danger">审核不通过</span>',
    	self::CHECK_STATUS_3 => '<span class="el-tag el-tag--info">赏金猎人申述</span>',
    	self::CHECK_STATUS_4 => '<span class="el-tag el-tag--info">悬赏主申述</span>'
    ];
    const R_STATUS_TEXT_1 = [
    		self::CHECK_STATUS_0 => '正常',
    		self::CHECK_STATUS_1 => '审核通过',
    		self::CHECK_STATUS_2 => '审核不通过',
    		self::CHECK_STATUS_3 => '赏金猎人申述',
    		self::CHECK_STATUS_4 => '悬赏主申述'
    ];
    
    protected $fillable = [
        'id','user_id',
    	'reward_id','status',
    	'check_status','submit_times','sumit_at',
    	'end_time','get_money',	
    	'created_at','updated_at','staus_end_time'
    ];

    public function detail_id(){
    	return $this->belongsTo('App\Models\OrderDetail','id','order_id');
    }
    public function user_id(){
    	return $this->belongsTo('App\Models\Users','user_id','id');
    }
    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'],  $page = null, $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
            	if(isset($params['user_id']) && $params['user_id']) {
            		$query ->where('user_id',$params['user_id']);
            	}
            	
            	if(isset($params['reward_id']) && $params['reward_id']) {
            		$query ->where('reward_id',$params['reward_id']);
            	}
            	
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                
                if(isset($params['check_status']) && $params['check_status']) {
                	$query ->where('check_status',$params['check_status']);
                }               
            })->orderBy('created_at', 'desc')
            ->paginate($limit);
	
        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
        	'r_status'=> self::R_STATUS_TEXT
        ]);

        return $lists;
    }

    
    
    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort='created_at', $sort_by='desc'){

    	if(isset($page)){
    		$lists = $this->select($fields)
    		->where(function ($query) use ($params) {
    		if(isset($params['user_id']) && $params['user_id']) {   				
            		$query ->where('user_id',$params['user_id']);
            	}
            	
            	if(isset($params['reward_id']) && $params['reward_id']) {
            		$query ->where('reward_id',$params['reward_id']);
            	}
            	
                if(isset($params['status']) && $params['status']>0) {
                   
                    
                    if($params['status'] == 4){
                    	//$params['status'] = ['>=','4'];
                    	$query ->whereIn('status',['4','5']);
                    }else if($params['status'] == 1){
                    		
                    	//$params['status'] = ['in','1,2'];
                    	$query ->whereIn('status',['1','2']);
                    }else{
                    	//$params['status'] = $params['status'];
                    	$query ->where('status',$params['status']);
                    }
                    
                }
                
                if(isset($params['check_status']) && $params['check_status']) {

                	$query ->where('check_status',$params['check_status']);
                }
    		})->orderBy($sort,$sort_by)->paginate($limit, null, null, $page);
    	}else{
    		$lists = $this->select($fields)
    		->where(function ($query) use ($params) {
    		if(isset($params['user_id']) && $params['user_id']) {
            		$query ->where('user_id',$params['user_id']);
            	}
            	
            	if(isset($params['reward_id']) && $params['reward_id']) {
            		$query ->where('reward_id',$params['reward_id']);
            	}
            	
                if(isset($params['status']) && $params['status']>0) {
                   
                    
                    if($params['status'] == 4){
                    	//$params['status'] = ['>=','4'];
                    	$query ->whereIn('status',['4','5']);
                    }else if($params['status'] == 1){
                    		
                    	//$params['status'] = ['in','1,2'];
                    	$query ->whereIn('status',['1','2']);
                    }else{
                    	$params['status'] = $state;
                    	$query ->where('status',$params['status']);
                    }
                    
                }
                
                if(isset($params['check_status']) && $params['check_status']) {
                	$query ->where('check_status',$params['check_status']);
                }
    		})->orderBy($sort,$sort_by)->paginate($limit)->get();
    
    	}
		
    	$this->stateToText($lists,[
    			'status'=> self::STATUS_TEXT,
    			'check_status'=> self::R_STATUS_TEXT
    	]);

    	return $lists;
    }
    
    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){
    	$info = $this->select($fields)->where($params)->first();
    	//获取任务步骤等信息
    	$map =[
           	'user_id' => $info['user_id'],
    		'reward_id' => $info['id']
    	];
    	$info['step_info'] = OrderDetail::where($map)->first();
    	
    	
        return $info;
    }

    
    
    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
    	//'任务分类','任务金额','任务总数','任务完成个数','发布人','任务状态'
    	$fields = [
    			'id','user_id','get_money','status','check_status',
    	];

    	$lists = $this->select($fields)
    	->where(function ($query) use ($params) {
    		if(isset($params) && $params) {
    			$ids = explode('-',$params);
    			$query ->whereIn('reward_id',$ids);
    		}
    	})->where('status','<>',-1)->get();
    	foreach ($lists as $k=>$v){
    		$user_info = Users::where('id',$v['user_id'])->first(['nickname','true_name']);
    		$v->attributes['user_id'] =  $user_info['true_name']? $user_info['true_name']:$user_info['nickname'];    		
    		$v->attributes['get_money'] = $v['get_money'];
  		
    		if($v->attributes['status'] == 1){
    			$v->attributes['status'] = '进行中';
    		}else if($v->attributes['status'] == 2){
    			$v->attributes['status'] = '已提交';
    		}else if($v->attributes['status'] == 3){
    			$v->attributes['status'] = '任务完成';
    		}else if($v->attributes['status'] == 4){
    			$v->attributes['status'] = '任务失败';
    		}else if($v->attributes['status'] == 5){
    			$v->attributes['status'] = '任务超时';
    		}else{
    			$v->attributes['status'] = '初始状态';
    		}

    		if($v->attributes['check_status'] == 1){
    			$v->attributes['check_status'] = '审核通过';
    		}else if($v->attributes['check_status'] == 2){
    			$v->attributes['check_status'] = '审核通过';
    		}else if($v->attributes['check_status'] == 3){
    			$v->attributes['check_status'] = '赏金猎人申述';
    		}else if($v->attributes['check_status'] == 4){
    			$v->attributes['check_status'] = '悬赏主申述';
    		}else{
    			$v->attributes['check_status'] = '未审核';
    		}

    		
   			$step = OrderDetail::where("order_id",$v['id'])->first();
    		$v->attributes['step1_user_desc'] = $step['step1_user_desc'];
    		$v->attributes['step1_image'] = $step['step1_image'];
    		
    		$v->attributes['step2_user_desc'] = $step['step2_user_desc'];
    		$v->attributes['step2_image'] = $step['step2_image'];
    		
    		$v->attributes['step3_user_desc'] = $step['step3_user_desc'];
    		$v->attributes['step3_image'] = $step['step3_image'];
    		
    		$v->attributes['step4_user_desc'] = $step['step4_user_desc'];
    		$v->attributes['step4_image'] = $step['step4_image'];
    		
    		$v->attributes['step5_user_desc'] = $step['step5_user_desc'];
    		$v->attributes['step5_image'] = $step['step5_image'];
    		
    		$v->attributes['step6_user_desc'] = $step['step6_user_desc'];
    		$v->attributes['step6_image'] = $step['step6_image'];
    
    	}
    	return $lists;
    }
}
